package br.com.sgi.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;

@Entity
@Table(name = "USERS", schema = "SGI")
@NamedQueries({

		@NamedQuery(name = "findByUsername", query = "SELECT usu FROM Users usu WHERE usu.username = :username"),

		@NamedQuery(name = "findAll", query = "SELECT usu FROM Users usu"),
		
		@NamedQuery(name = "buscarUsersPorCoSistema", 
		query = "SELECT usu FROM Users usu " +
				" WHERE usu.sistema.coSeqSistema = :coSistema")

})
public class Users implements Serializable, UserDetails {

	private static final long serialVersionUID = -8451679170281063697L;

	@Id
	@NotNull
	@Size(min = 1, max = 50)
	@Column(name="USERNAME",unique = true)
	private String username;

	@NotNull
	@Size(min = 1, max = 50)
	private String password;

	@OneToMany(mappedBy="username",  fetch = FetchType.EAGER, cascade={CascadeType.ALL})
	private List<Authorities> authorities;

	@NotNull
	private boolean enabled;
	
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="CO_SISTEMA")
	private Sistema sistema;
	
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="CO_IGREJA")
	private Igreja igreja; 
	
	@OneToMany(mappedBy="users", cascade={CascadeType.ALL})
	private List<UsersIgreja> igrejasAutorizadas;
	
	
	@Size(min = 1, max = 100)
	private String nome;
	
	
	@Size(min = 1, max = 100)
	private String email;

	@Transient
	public Collection<GrantedAuthority> getAuthorities() {

		List<GrantedAuthority> result =	new ArrayList<GrantedAuthority>();
		for (Authorities aut : authorities) {
			result.add(new GrantedAuthorityImpl(aut.getAuthority()));
		}
		return result;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setAuthorities(List<Authorities> authorities) {
		this.authorities = authorities;
	}

	@Transient
	public boolean isAccountNonExpired() {
		return true;
	}

	@Transient
	public boolean isAccountNonLocked() {
		return true;
	}

	@Transient
	public boolean isCredentialsNonExpired() {
		return true;
	}

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

	public Sistema getSistema() {
		return sistema;
	}

	public void setSistema(Sistema sistema) {
		this.sistema = sistema;
	}

	public List<UsersIgreja> getIgrejasAutorizadas() {
		return igrejasAutorizadas;
	}

	public void setIgrejasAutorizadas(List<UsersIgreja> igrejasAutorizadas) {
		this.igrejasAutorizadas = igrejasAutorizadas;
	}

	public Igreja getIgreja() {
		return igreja;
	}

	public void setIgreja(Igreja igreja) {
		this.igreja = igreja;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
}
